import {
  carouselImgList,
  getContentList
} from "../../api/cm";
import {
  getUserList
} from "../../api/um";
import {
  getActList
} from "../../api/act";

const app = getApp();
const globalData = app.globalData;
const {
  config,
  curDept,
  eventBus,
  colorOption
} = globalData;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    config,
    colorOption,
    curDept,
    // 轮播图
    bannerList: [],
    // 快捷模块
    shortcutList: [{
        icon: "/images/index/type1.png",
        text: "婚恋学堂",
        type: "content",
        categoryId: '37060049391000198',
      },
      {
        icon: "/images/index/type2.png",
        text: "爱情汇客厅",
        type: "activity",
      },
      {
        icon: "/images/index/type3.png",
        text: "甜蜜地图",
        type: "content",
        categoryId: '37060081532000140',
      },
      {
        icon: "/images/index/type4.png",
        text: "每日签到",
        type: "sign",
      },
    ],
    // 活动动态列表
    activityList: [],
    // 推荐嘉宾列表
    recommendedList: [],
    categoryLoading: true,
    // 文章列表
    contentList: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.initPage();
    app.getAttrConfList();

    // 选择地区组件
    this.regionComponent = this.selectComponent("#regionComponent");

    // test 跳转认证
    // wx.navigateTo({
    //   url: "/pageA/pages/verified/index",
    // });
    this.getImgs();
    this.getActList();
    this.getRecommendedList();
    this.getContentList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setSelected(0);
    }
    this.setData({
      curDept: globalData.curDept,
    });
  },
  getContentList() {
    this.setData({
      categoryLoading: true,
      contentList: []
    });
    getContentList({
      data: {
        pageSize: 3,
        categoryId: '37060119661000101'
      },
      callBack: (res) => {
        this.setData({
          contentList: res.rows.map(item => {
            item.contentImg = item.contentImg ? config.localImgSrc + item.contentImg : '';
            item.distance = item.distance ? item.distance.toFixed(2) : '--';
            return item;
          })
        })
        this.setData({
          categoryLoading: false
        });
      },
    });
  },
  getImgs() {
    carouselImgList({
      data: {
        type: 'home'
      },
      callBack: (res) => {
        this.setData({
          bannerList: res.data.map(item => {
            item.imgUrl = config.localImgSrc + item.imgUrl;
            return item
          })
        })
      },
    })
  },
  // 往期活动
  getActList() {
    getContentList({
      data: {
        categoryId: '37143301093000172',
        orderByColumn: 'createTime',
        order: 'desc',
        pageSize: 6
      },
      callBack: (res) => {
        this.setData({
          activityList: res.rows.map(item => {
            item.contentImg = item.contentImg ? config.localImgSrc + item.contentImg : '';
            item.createTime = item.createTime.substr(0, 10)
            return item
          })
        })
      },
    })
  },
  //   推荐嘉宾
  getRecommendedList() {
    getUserList({
      data: {
        isShow: 1,
        pageSize: 3,
      },
      callBack: (res) => {
        res.rows && res.rows.length && res.rows.forEach(item => {
          item.attrDataMap.TX = item.attrDataMap.TX ? config.localImgSrc + item.attrDataMap.TX : (item.attrDataMap.XB == 1 ? '/images/mine/defaultAvatar_male.png' : '/images/mine/defaultAvatar_female.png');
        })
        this.setData({
          recommendedList: res.rows
        })
      },
    })
  },
  goAct(e) {
    const {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pageA/pages/contentDetail/index?id=${item.id}&title=往期活动`,
    })
  },
  swiperTap(e) {
    let {
      item
    } = e.detail.currentTarget.dataset;
    if (item.contentId) {
      wx.navigateTo({
        url: `/pageA/pages/contentDetail/index?id=${item.contentId}&title=怀青有约`,
      })
    } else if (item.actId) {
      wx.navigateTo({
        url: `/pageA/pages/activityDetail/index?id=${item.actId}`,
      })
    }
  },
  goContentList() {
    eventBus.emit("shortcutItem", {
      categoryId: '37060119661000101',
      text: '甜蜜展示'
    });
    wx.navigateTo({
      url: "/pageA/pages/contentList/index",
    });
  },
  // 选择地区
  handleRegionSelect(e) {
    this.setData({
      curDept: globalData.curDept,
    });
    this.getRecommendedList()
  },
  // 认证
  handleAuth(e) {
    this.setData({showAuth: true, authStatus: globalData.userInfo.authStatus})
  },
  handleCloseAuth(){
    this.setData({showAuth: false})
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});